package com.hongxun.design.adapters;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.CreateProjectActivity;
import com.hongxun.design.activitys.CreateProjectAttributeActivity;
import com.hongxun.design.bean.CreateProjectBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.utils.InputUtil;
import com.hongxun.design.utils.PopwindowUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zerdoor_pc on 2016/4/29.
 */
public class CreateProjectAdapter extends BaseAdapter {
    private List<CreateProjectBean> beanList;
    LayoutInflater inflater;
    Activity context;
    String ss = "";
    private int index = -1;
    public int mPosition = -1;
    public Map<String, Integer> map;
    int bluecolor;
    int graycolor;

    public CreateProjectAdapter(List<CreateProjectBean> beanList, Activity context) {
        this.beanList = beanList;
        this.context = context;
        bluecolor = context.getResources().getColor(R.color.project_blue);
        graycolor = context.getResources().getColor(R.color.gray);
        inflater = LayoutInflater.from(context);
        map = new HashMap<>();
    }

    public List<CreateProjectBean> getData() {
        return beanList;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        int type = Constant.CREATE_GENERAL;
        switch (beanList.get(position).getItemType()) {
            case Constant.CREATE_GENERAL:
                type = Constant.CREATE_GENERAL;
                break;
            case Constant.CREATE_REMARKS:
                type = Constant.CREATE_REMARKS;
                break;
        }
        return type;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        int ViewType = getItemViewType(position);
        GeneralHolder generalHolder = null;
        RemarkslHolder remarkslHolder = null;
        CreateProjectBean bean;

        switch (ViewType) {
            case Constant.CREATE_GENERAL:
                convertView = inflater.inflate(R.layout.listview_item_create_generl, null);
                generalHolder = new GeneralHolder(convertView);
                convertView.setTag(generalHolder);
                break;
            case Constant.CREATE_REMARKS:
                convertView = inflater.inflate(R.layout.listview_item_create_remarks, null);
                remarkslHolder = new RemarkslHolder(convertView);
                convertView.setTag(remarkslHolder);
                break;
        }

        switch (ViewType) {
            case Constant.CREATE_GENERAL:
                ss = "";
                bean = beanList.get(position);
                if (bean.getTypeNum() == 0) {
                    generalHolder.create_type.setText(bean.getTypeName());
                    generalHolder.create_type.setVisibility(View.VISIBLE);
                } else {
                    generalHolder.create_type.setVisibility(View.GONE);
                }
                generalHolder.create_name.setText(bean.getName());//项名称
                //判断是否为输入框
                switch (bean.getIsWrite()) {
                    case Constant.CLICKEDTEXT:
                        setEditText(generalHolder, bean);
                        generalHolder.create_write.setInputType(InputType.TYPE_CLASS_TEXT);
                        generalHolder.create_write.setOnTouchListener(new View.OnTouchListener() {

                            public boolean onTouch(View view, MotionEvent event) {
                                if (event.getAction() == MotionEvent.ACTION_UP) {
                                    index = position;
                                }
                                return false;
                            }
                        });
                        generalHolder.create_write.clearFocus();
                        if (index != -1 && index == position) {
                            generalHolder.create_write.requestFocus();
                        }
                        break;
                    case Constant.CLICKPOP:
                        setPop(generalHolder, bean);
                        break;
                    case Constant.CLICKACTIVITY:
                        map.put(bean.getActivityType(), Integer.parseInt(position + ""));
                        setActivity(generalHolder, bean, position);
                        break;
                }
                break;
            case Constant.CREATE_REMARKS:
                bean = beanList.get(position);
                if (bean.getTypeNum() == 0) {
                    remarkslHolder.create_type.setText(bean.getTypeName());
                    remarkslHolder.create_type.setVisibility(View.VISIBLE);
                } else {
                    remarkslHolder.create_type.setVisibility(View.GONE);
                }
                remarkslHolder.create_remarks.setText(bean.getValue());
                remarkslHolder.create_remarks.setTextColor(graycolor);
                remarkslHolder.create_remarks.addTextChangedListener(bean.getTextWatcher());

                remarkslHolder.create_remarks.setOnTouchListener(new View.OnTouchListener() {

                    public boolean onTouch(View view, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            index = position;
                        }
                        return false;
                    }
                });
                remarkslHolder.create_remarks.clearFocus();
                if (index != -1 && index == position) {
                    // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
                    remarkslHolder.create_remarks.requestFocus();
                }
                break;
        }
        return convertView;

    }

    //设置edtext类的数据
    private void setEditText(GeneralHolder generalHolder, CreateProjectBean bean) {
        generalHolder.create_write.setVisibility(View.VISIBLE);
        generalHolder.create_value.setVisibility(View.GONE);
        generalHolder.create_right.setVisibility(View.GONE);
        if (bean.getValue().length() != 0) {
            generalHolder.create_write.setText(bean.getValue());
            generalHolder.create_write.setTextColor(graycolor);
        } else {
            if (bean.ismustInput()) {
                generalHolder.create_write.setHint(MString.NoValue);
                generalHolder.create_write.setHintTextColor(bluecolor);
            } else {
                generalHolder.create_write.setHint(MString.AllowNo);
                generalHolder.create_write.setHintTextColor(graycolor);
            }
        }
        if (bean.getSendCode().equals(MString.manager)) {
            generalHolder.create_write.setEnabled(false);
        } else {
            generalHolder.create_write.setEnabled(true);
        }
        generalHolder.create_write.addTextChangedListener(bean.getTextWatcher());
    }

    private void setOtherHead(GeneralHolder generalHolder, CreateProjectBean bean) {
        generalHolder.create_write.setVisibility(View.GONE);
        generalHolder.create_value.setVisibility(View.VISIBLE);
        generalHolder.create_right.setVisibility(View.VISIBLE);

        if (bean.getValue() == null || bean.getValue().length() == 0) {
            if (bean.getValuearr() != null && bean.getValuearr().length > 0) {
                generalHolder.create_value.setText(bean.getValuearr()[0]);
                generalHolder.create_value.setTextColor(graycolor);
            } else if (bean.ismustInput()) {
                generalHolder.create_value.setText(MString.NoChoice);
                generalHolder.create_value.setTextColor(bluecolor);
            } else {
                generalHolder.create_value.setText(MString.AllowNo);
                generalHolder.create_value.setTextColor(graycolor);
            }
        } else {
            generalHolder.create_value.setText(bean.getValue());
            generalHolder.create_value.setTextColor(graycolor);
        }

    }

    //设置弹出框类的数据
    private void setPop(final GeneralHolder generalHolder, final CreateProjectBean bean) {
        setOtherHead(generalHolder, bean);
        generalHolder.create_right.setTextColor(graycolor);
        SetTypeface.set(generalHolder.create_right, IconSourse.DROPDOWN);//RIGHTCHOICE
        generalHolder.create_item_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InputUtil.closeInput(context);
                WindowManager.LayoutParams params = context.getWindow().getAttributes();
                params.alpha = 0.7f;
                context.getWindow().setAttributes(params);
                PopwindowUtil.popChoice(context, generalHolder.create_value, bean);
            }
        });

    }

    //设置跳转界类的数据
    private void setActivity(GeneralHolder generalHolder, CreateProjectBean bean, int i) {
        setOtherHead(generalHolder, bean);
        if (bean.getActivityType().equals(Constant.ACTIVITYMAP)) {
            generalHolder.create_right.setTextColor(bluecolor);
            SetTypeface.set(generalHolder.create_right, IconSourse.LOCATION);
        } else {
            generalHolder.create_right.setTextColor(graycolor);
            SetTypeface.set(generalHolder.create_right, IconSourse.RIGHTCHOICE);
        }

        generalHolder.create_item_button.setOnClickListener(new MClickListener(i, bean) {
            @Override
            public void onClick(View view, int position, Object msg) {
                CreateProjectBean bean = (CreateProjectBean) msg;
                InputUtil.closeInput(context);
                Bundle bundle = new Bundle();
                bundle.putString(MString.TYPE, bean.getActivityType());
                bundle.putString(MString.TYPENUM, position + "");
                if (bean.getActivityType().equals(Constant.ACTIVITYMULTISELECT)) {
                    bundle.putString(MString.TITLE, bean.getName());
                    switch (bean.getName()) {
                        case Constant.项目负责人:
                            bundle.putString(MString.SENDCODE, "11");
                            break;
                        case Constant.项目参与人:
                            bundle.putString(MString.SENDCODE, "12");
                            break;
                    }
                    bundle.putString(MString.userid, bean.getValuecode());
                    if (((CreateProjectActivity) context).type.equals(MString.ISEDIT)) {
                        bundle.putString(MString.stage_id, ((CreateProjectActivity) context).stage_id);
                    }
                    if (bean.getName().equals(Constant.项目负责人)) {
                        if (MyApp.userInfo.getOrg_type().equals("10")) {
                            ToastShow.showShort(context, "抱歉，您不是单位用户");
                            return;
                        }
                    }
                } else if (bean.getActivityType().equals(Constant.ACTIVITYRADIO)) {
                    bundle.putString(MString.TITLE, bean.getName());
                    if (Constant.设计单位.equals(bean.getName())) {
                        bundle.putString(MString.SENDCODE, "12");
                    } else {
                        bundle.putString(MString.SENDCODE, "11");
                    }
                }
                IntentToActivity.intent(context, CreateProjectAttributeActivity.class, bundle);
            }
        });
    }

    class GeneralHolder {
        public TextView create_name, create_value, create_right, create_type;
        public EditText create_write;
        public RelativeLayout create_item_button;

        GeneralHolder(View convertView) {
            create_item_button = (RelativeLayout) convertView.findViewById(R.id.create_item_button);
            create_type = (TextView) convertView.findViewById(R.id.create_type);

            create_name = (TextView) convertView.findViewById(R.id.create_name);
            create_write = (EditText) convertView.findViewById(R.id.create_write);
            create_value = (TextView) convertView.findViewById(R.id.create_value);
            create_right = (TextView) convertView.findViewById(R.id.create_right);
        }
    }

    public class RemarkslHolder {
        public TextView create_type;
        public EditText create_remarks;

        RemarkslHolder(View convertView) {
            create_type = (TextView) convertView.findViewById(R.id.create_type);
            create_remarks = (EditText) convertView.findViewById(R.id.create_remarks);
        }
    }
}
